//
//  ProtectListViewController.m
//  BMS
//
//  Created by 颜魏 on 2024/12/9.
//

#import "ProtectListViewController.h"
#import "ProtectTableViewCell.h"

@interface ProtectListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *firstLevelLabel;
@property (weak, nonatomic) IBOutlet UILabel *secondLevelLabel;
@property (weak, nonatomic) IBOutlet UILabel *threeLevelLabel;

@property (nonatomic, strong) NSMutableArray *protectionArray;
@property (nonatomic, strong) NSMutableArray *protectValue;

@end

@implementation ProtectListViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setLanguage];
    self.protectionArray = [[NSMutableArray alloc] init];
    self.protectValue = [[NSMutableArray alloc] init];

    [self.protectValue addObject:NSLocalizedString(@"protectInfo_0", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_1", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_2", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_3", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_4", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_5", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_6", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_7", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_8", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_9", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_10", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_11", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_12", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_13", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_14", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_15", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_16", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_17", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_18", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_19", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_20", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_21", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_22", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_23", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_24", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_25", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_26", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_27", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_28", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_29", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_30", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_31", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_32", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_33", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_34", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_35", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_36", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_37", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_38", nil)];
    [self.protectValue addObject:NSLocalizedString(@"protectInfo_39", nil)];

    [self.tableView registerNib:[UINib nibWithNibName:@"ProtectTableViewCell" bundle:nil] forCellReuseIdentifier:@"ProtectTableViewCell"];
}

- (void)setLanguage {
    self.titleLabel.text = NSLocalizedString(@"protectionRecord",nil);
    [self.rightBtn setTitle:NSLocalizedString(@"resetProtection",nil) forState:UIControlStateNormal];
    self.firstLevelLabel.text = NSLocalizedString(@"firstProtection",nil);
    self.secondLevelLabel.text = NSLocalizedString(@"secondProtection",nil);
    self.threeLevelLabel.text = NSLocalizedString(@"thirdProtection",nil);
}

- (IBAction)actionBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)actionClear:(id)sender {
    [self.protectionArray removeAllObjects];
    [self.tableView reloadData];
}

#pragma mark - notification
- (void)didReciveData:(NSNotification *)notification {
    NSData *reciveData = notification.object;
    Byte *dataBytes = (Byte*)reciveData.bytes;
    if (dataBytes[2]==0x2a) {
        NSLog(@"receieve protect data: %@",reciveData);
        //3.3 主控读取时钟及保护记录等信息
        [self.protectionArray removeAllObjects];
        [self.protectionArray addObject:[NSString stringWithFormat:@"%i", dataBytes[3 + 0x03 * 2 + 0]]];
        [self.protectionArray addObject:[NSString stringWithFormat:@"%i", dataBytes[3 + 0x03 * 2 + 4]]];
        [self.protectionArray addObject:[NSString stringWithFormat:@"%i", dataBytes[3 + 0x03 * 2 + 8]]];
        [self.protectionArray addObject:[NSString stringWithFormat:@"%i", dataBytes[3 + 0x03 * 2 + 1]]];
        [self.protectionArray addObject:[NSString stringWithFormat:@"%i", dataBytes[3 + 0x03 * 2 + 5]]];
        [self.protectionArray addObject:[NSString stringWithFormat:@"%i", dataBytes[3 + 0x03 * 2 + 9]]];
        [self.protectionArray addObject:[NSString stringWithFormat:@"%i", dataBytes[3 + 0x03 * 2 + 2]]];
        [self.protectionArray addObject:[NSString stringWithFormat:@"%i", dataBytes[3 + 0x03 * 2 + 6]]];
        [self.protectionArray addObject:[NSString stringWithFormat:@"%i", dataBytes[3 + 0x03 * 2 + 10]]];
        [self.protectionArray addObject:[NSString stringWithFormat:@"%i", dataBytes[3 + 0x03 * 2 + 3]]];
        [self.protectionArray addObject:[NSString stringWithFormat:@"%i", dataBytes[3 + 0x03 * 2 + 7]]];
        [self.protectionArray addObject:[NSString stringWithFormat:@"%i", dataBytes[3 + 0x03 * 2 + 11]]];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    }
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.protectionArray.count/3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ProtectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ProtectTableViewCell"];
    NSString *position = [self.protectionArray objectAtIndex:indexPath.row*3];
    NSString *positionMiddle = [self.protectionArray objectAtIndex:indexPath.row*3+1];
    NSString *positionright = [self.protectionArray objectAtIndex:indexPath.row*3+2];
    cell.firstLevel = [self.protectValue objectAtIndex:position.integerValue];
    cell.seconfLevel = [self.protectValue objectAtIndex:positionMiddle.integerValue];
    cell.threeLevel = [self.protectValue objectAtIndex:positionright.integerValue];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

@end
